package day04.am;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.Random;

/**
 * 窗体类
 */
public class Demo01 {
    public static void main(String[] args) throws Exception {
        JFrame jFrame = new JFrame("捕鱼达人");
        jFrame.setSize(800,480);
        jFrame.setLocationRelativeTo(null);
        jFrame.setResizable(false);
        jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Pool pool = new Pool();
        jFrame.add(pool);

        jFrame.setVisible(true);
        //调用游戏开始的方法action
        pool.action();
    }
}//Demo01结束

//Pool开始
class Pool extends JPanel {
    BufferedImage bufferedImage;//背景图
    Fish[] fish;//鱼数组
    Net net;//渔网
    int count;//捕到的鱼的数量
    int score;//捕鱼获得的分数
    /**构造方法*/
    public Pool() throws IOException {
        bufferedImage = ImageIO.read(
                new File("fish/images/bg.jpg"));
        net = new Net();
        fish = new Fish[9+9+9+4];
        for(int i=0;i<9;i++){
            fish[i] = new Fish("fish0"+(i+1));
            fish[i+9] = new Fish("fish0"+(i+1));
            fish[i+9+9] = new Fish("fish0"+(i+1));
        }
        fish[fish.length-4] = new Fish("fish13");
        fish[fish.length-3] = new Fish("fish14");
        fish[fish.length-2] = new Fish("fish13");
        fish[fish.length-1] = new Fish("fish14");
    }

    /**绘图方法*/
    @Override
    public void paint(Graphics g) {
        g.drawImage(bufferedImage,0,0,null);
        if(net.show){//渔网处于显示状态就把他画出来
            //计算x1,y1坐标
            int x1 = net.x - net.width/2;
            int y1 = net.y - net.height/2;
            g.drawImage(net.image,x1,y1,null);
        }
        //绘制数量和分数
        g.setColor(Color.WHITE);
        Font f = new Font("宋体",Font.BOLD,15);
        g.setFont(f);
        g.drawString("捕鱼数量:0条",20,30);
        g.drawString("捕鱼得分:0分",20,60);
        for(int i=0;i<fish.length;i++){
            g.drawImage(fish[i].image,fish[i].x,fish[i].y,null);
        }
    }
    /**开始的方法*/
    public void action() throws InterruptedException {
        while(true){
            for(int i=0;i<fish.length;i++){
                fish[i].move();
            }
            Thread.sleep(20);
            repaint();
        }
    }
}//Pool结束

//Fish开始
class Fish{
    BufferedImage[] images;//鱼的10张图片数组
    BufferedImage image;//鱼的当前图片
    int width;//图片宽
    int height;//图片高
    int x;
    int y;
    int speed;//速度
    int index;//计数用

    public Fish(String name) throws IOException {
        images = new BufferedImage[10];
        for(int i=0;i<images.length;i++){
            String path = "fish/images/"+name+"_0"+i+".png";
            images[i] = ImageIO.read(new File(path));
        }
        image = images[0];
        width = image.getWidth();
        height = image.getHeight();
        Random r = new Random();
        x = r.nextInt(800-width);
        y = r.nextInt(480-height);
        speed = r.nextInt(5)+1;
    }
    /**鱼游动的方法*/
    public void move(){
        image = images[index++%10];
        x-=speed;
        if(x<-width){
            x=800;
            Random r = new Random();
            y = r.nextInt(480-height);
            speed = r.nextInt(5)+1;
        }
    }


}//Fish结束

//Net开始
class Net{
    BufferedImage image;//渔网图片
    int width;
    int height;
    int x;//渔网中心点坐标
    int y;
    boolean show;//是否显示 true显示

    public Net() throws IOException {
        image = ImageIO.read(
                new File("fish/images/net09.png"));
        width = image.getWidth();
        height = image.getHeight();
        Random r = new Random();
        x = r.nextInt(800-width);
        y = r.nextInt(480-height);
        show = true;
    }

    /**捕鱼的方法*/

    /**移动的方法*/




}//Net结束








